#include <iostream>
#include <vector>
using namespace std;

struct hun {
    int C;
    int D;
};

int maxh;
int baolv;
bool v[6][35];

void dfs(int p, vector<vector<hun>>& mm, int C, int hurt) {
    if (p == 7) {
        if (C < 100) return;
        maxh = max(maxh, hurt);
        return;
    }

    if (C >= 100) {
        int aaa = 0;
        for (int i = 0; i < mm[p].size(); i++) {
            aaa = max(aaa, mm[p][i].D);
        }
        dfs(p + 1, mm, 100, hurt + aaa);
    } else {
        for (int i = 0; i < mm[p].size(); i++) {
            dfs(p + 1, mm, C + mm[p][i].C, hurt + mm[p][i].D);
        }
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        maxh = -1;
        baolv = 0;
        vector<vector<hun>> mm(8);
        for (int i = 0; i < n; i++) {
            int P, C, D;
            cin >> P >> C >> D;
            hun h = {C, D};
            mm[P].push_back(h);
        }
        dfs(1, mm, 0, 0);
        if (maxh == -1)
            cout << "TAT" << endl;
        else
            cout << maxh << endl;
    }
    return 0;
}